<template>
    <div class="app-container">
        <el-form ref="queryParamsRef" :model="queryParams" label-width="auto">

            <el-row>
                <el-col style="flex: 1 1 0;">
                    <el-row :gutter="16">
                        <el-col :span="8">
                            <el-form-item label="预案名称：" prop="planname">
                                <el-input v-model="queryParams.planname" clearable @change="getList"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="预案类型：" prop="plantype">
                                <el-select v-model="queryParams.plantype" placeholder="选择预案类型" @change="getList">
                                    <el-option value="">全部</el-option>
                                    <el-option v-for="item in nj_yuan_type" :key="item.value" :label="item.label"
                                               :value="item.value"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="重点对象：" prop="peopleId">
                                <keyPersonManageSelect v-model="queryParams.peopleId" @change="getList"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="修订日期：">
                                <el-date-picker v-model="searchTime" end-placeholder="截止时间" range-separator="-"
                                                start-placeholder="开始时间" type="daterange"
                                                value-format="YYYY-MM-DD"
                                                @change="changeDateTimerHandle" @clear="changeDateTimerHandle"/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-col>
                <el-divider direction="vertical" style="height: 84px"/>
                <el-col style="flex: 0 0 86px;">
                    <el-space :size="18" direction="vertical">
                        <el-button type="primary" @click="getList">
                            <el-icon class="me-1">
                                <Search/>
                            </el-icon>
                            查询
                        </el-button>
                        <el-button @click="resetQueryParamsHandle(queryParamsRef)">
                            <el-icon class="me-1">
                                <Refresh/>
                            </el-icon>
                            重置
                        </el-button>
                    </el-space>
                </el-col>
            </el-row>

        </el-form>

        <el-divider class="mt-0"/>

        <el-table v-loading="loading" :data="tableData" border stripe
        :row-style="tableRowStyle" @row-dblclick="handleDbClick">
            <el-table-column label="预案名称" prop="planname"/>
            <el-table-column label="单位名称" prop="unitname"/>
            <el-table-column label="部门名称" prop="depId"/>
            <el-table-column label="预案类型" prop="plantype"/>
            <el-table-column label="修订日期" prop="adddate"/>
            <el-table-column label="状态" prop="stateText" width="180"/>
            <el-table-column label="操作" width="100">
                <template #default="scope">
                    <el-button link size="small" type="primary" @click="planDetailRef.showDetail(scope.row)">
                        <el-icon class="me-1">
                            <View/>
                        </el-icon>
                        详情
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" v-model:limit="paging.pageSize" v-model:page="paging.pageNum" :total="total"
                    @pagination="getList"/>

        <PlanDetail ref="planDetailRef"/>
    </div>
</template>

<script setup>
import {Refresh, Search, View} from '@element-plus/icons-vue';
import {useTemplateRef} from 'vue';
import {getUnitPlanList} from '@/api/PlanManage/Plan.js';
import PlanDetail from '@/views/PlanManage/PlanSearch/components/PlanDetail.vue';
import KeyPersonManageSelect from '@/components/KeyPersonManageSelect/KeyPersonManageSelect.vue';
import PageEnum from '@/enum/PageEnum.js';

const {proxy} = getCurrentInstance()
const {nj_yuan_type} = proxy.useDict('nj_yuan_type')

const loading = ref(false)
const total = ref(0)
const paging = ref({
    // 页码
    pageNum: 1,
    // 分页数量
    pageSize: PageEnum.SIZE
})

const searchTime = ref([])
const queryParams = ref({
    // 预案名称
    planname: '',
    // 预案类型
    plantype: '',
    params: {
        'beginTime': '',
        'endTime': ''
    }
})

const tableData = ref([])
const queryParamsRef = useTemplateRef('queryParamsRef')
const planDetailRef = useTemplateRef('planDetailRef')

onMounted(() => getList())
// 斑马纹样式函数
const tableRowStyle = ({ row, rowIndex }) => {
    if (rowIndex % 2 === 0) {
        return { backgroundColor: '#e6ecff' }; // 偶数行背景色
    }
    return {}; // 奇数行默认透明
}
// 获取列表
const getList = async () => {
    loading.value = true
    const res = await getUnitPlanList(queryParams.value, paging.value)
    tableData.value = res.rows
    total.value = res.total
    loading.value = false
}

// 改变时间
const changeDateTimerHandle = (date) => {
    console.log(date)
    if (date) {
        queryParams.value = {
            ...queryParams.value,
            params: {
                beginTime: date[0],
                endTime: date[1]
            }
        }
    } else {
        queryParams.value = {
            ...queryParams.value,
            params: {
                beginTime: '',
                endTime: ''
            }
        }
    }
    getList()
}

// 重置搜索
const resetQueryParamsHandle = async (elForm) => {
    if (!elForm) return
    elForm.resetFields()
    searchTime.value = []
    queryParams.value.params.beginTime = ''
    queryParams.value.params.endTime = ''
    await getList()
}

const handleDbClick = (row) => {
    planDetailRef.value.showDetail(row)
}
</script>

<style lang="scss" scoped>
/* 保留斑马纹的同时添加悬停效果 */
:deep().el-table--striped .el-table__body tr.el-table__row--striped:hover td {
  background-color: #a5b8f4; /* 悬停时的红色 */
}

:deep().el-table__body tr:hover td {
  background-color: #a5b8f4; /* 悬停时的红色 */
}</style>